package com.wzdigit.wms.wms.service.core.transfer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.wms.basic.domain.transfer.TransferDtl;
import com.wzdigit.wms.basic.mapper.core.transfer.TransferDtlMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 调拨单明细 服务类
 * </p>
 *
 * @author
 * @since 2021-01-19
 */
@Service
@Slf4j
public class TransferDtlService {

    @Autowired
    TransferDtlMapper transferDtlMapper;

    /**
     * 根据入参实体新增一条记录
     *
     * @param transferDtl
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public TransferDtl addNewOne(TransferDtl transferDtl) {
        int row = transferDtlMapper.insert(transferDtl);
        log.info("成功新增{}条记录", row);

        return transferDtl;
    }

    /**
     * 根据入参实体更新一条记录
     *
     * @param transferDtl
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public TransferDtl updateOne(TransferDtl transferDtl) {
        int row = transferDtlMapper.updateById(transferDtl);
        log.info("成功更新{}条记录", row);

        return transferDtl;
    }

    /**
     * 根据明细主键和行号,调拨单号查询
     *
     * @param transferDtlId
     * @param transferLineNo
     * @return
     */
    public TransferDtl getTransferTaskDtlByPKAndLineNo(Integer transferDtlId, String transferLineNo, String transferNo) {

        //创建类型的条件构造器
        QueryWrapper<TransferDtl> wrapper = new QueryWrapper<>();
        wrapper.eq("TRANSFER_DTL_ID", transferDtlId);
        wrapper.eq("TRANSFER_LINE_NO", transferLineNo);
        wrapper.eq("TRANSFER_NO", transferNo);

        TransferDtl transferDtl = transferDtlMapper.selectOne(wrapper);
        return transferDtl;
    }

    /**
     * 根据明细主键查询
     * @param transferDtlId
     * @return
     */
    public TransferDtl getTransferTaskDtlByPK(Long transferDtlId) {
        //创建类型的条件构造器
        QueryWrapper<TransferDtl> wrapper = new QueryWrapper<>();
        wrapper.eq("TRANSFER_DTL_ID", transferDtlId);

        TransferDtl transferDtl = transferDtlMapper.selectOne(wrapper);
        return transferDtl;
    }

    /**
     * 根据单头查询明细集合
     * @param transferHeaderId
     * @return
     */
    public List<TransferDtl> getByHeaderId(Integer transferHeaderId) {

        //创建类型的条件构造器
        QueryWrapper<TransferDtl> wrapper = new QueryWrapper<>();
        wrapper.eq("TRANSFER_HEADER_ID",transferHeaderId );

        List<TransferDtl> transferDtlList = transferDtlMapper.selectList(wrapper);
        return transferDtlList;
    }

    /**
     *
     * @param docDtlId
     * @param picktaskFlag
     * @param loginName
     * @return
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public Integer updateDtlPicktaskFlag(Long docDtlId, int picktaskFlag, String loginName) {

        TransferDtl transferDtl = new TransferDtl();
        transferDtl.setModifyUser(loginName);
        transferDtl.setModifyTime(new Date());

        transferDtl.setTransferDtlId(docDtlId).setPickTaskFlag(picktaskFlag);

        int row = transferDtlMapper.updateById(transferDtl);
        return row;
    }
}
